/*--------------Running--------------------------*/
var infinit,
	loadimg2,
	widgetID,
	flickrID,
	Modernizr,
	template,
    number,
	jQuery;
	
/*jshint smarttabs:true */
/* jshint -W099 */
jQuery(document).ready(function($){
  "use strict";
	
  /*Init*/
  var documentHeight=$('body').height(),
      windowHeight=$(window).height();
  $('#sidebar').css('min-height',documentHeight+'px');
  
  /*Scroll*/
  $(window).scroll(function () {
	  
	  //SIDEBAR
	  var documentHeight = $('body').height(),
	      sidebarHeight =$('#sidebar').height();
	  function checkWindowHeight(){
		    if( sidebarHeight !== documentHeight){
		      $('#sidebar').css('min-height',documentHeight+'px');
		    }
	  } 
	  checkWindowHeight();
	  $(window).resize(checkWindowHeight);
  
	  //** Translucent background for the portfolio filter when scroll down the page.
	  $('nav.portfolio-navigation').css('background','#000');
	  if($(document).scrollTop()===0){
		$('nav.portfolio-navigation').css('background','none');
	  }
  });
  
  //ISOTOPE
  var $container = $('#portfolio-items');
  $container.hide();
  $(window).load(function(){
        $('.loading').hide();
	$container.css('display','block');
    $container.isotope({
		filter: '*',
		animationOptions: {
		  duration: 750,
		  easing: 'linear',
		  queue: false,
		}
    },function(){$('.van-pagenavi').fadeIn();});
  });
  
  $('#portfolio-filter a').click(function(){
    var selector = $(this).attr('data-filter');
    $container.isotope({
      filter: selector,
      animationOptions: {
        duration: 750,
        easing: 'linear',
        queue: false,
      }
    });
    return false;
  });

  var $optionSets = $('#portfolio-filter'),
      $optionLinks = $optionSets.find('a');
   
         $optionLinks.click(function(){
            var $this = $(this);
      // dont proceed if already selected
      if ( $this.hasClass('current-filter') ) {
          return false;
      }
     var $optionSet = $this.parents('#portfolio-filter');
     $optionSet.find('.current-filter').removeClass('current-filter');
     $this.addClass('current-filter'); 
  });

  /* FLUID ARTICLES */
  var boxwidth,
      $fluid = $('.fluid-articles');
  $fluid.children('.box').hide();
  if($(window).width() <= 800) { boxwidth=45;}else{ boxwidth=100;}
     $(window).load(function(){
	  $fluid.children('.box').fadeIn();
	  if(infinit===0){
	    $('.van-pagenavi').fadeIn();
	  }
	  $('.loading').fadeOut();
	  if($(window).width() > 640) {
		  $fluid.masonry({
			 itemSelector : '.box',
			 isAnimated: true,
			 columnWidth:boxwidth
		  });
	  }

	 if(infinit===1){
		 $fluid.infinitescroll({
		   navSelector  : '.van-pagenavi',    
		   nextSelector : '.van-pagenavi > a', 
		   itemSelector : '.box', 
		   loading: {
			  finishedMsg: "I think we've hit the end.",
			  msgText: "Loading More",
			  img: loadimg2,
			 }
		   },
		   // trigger Masonry as a callback
		   function( newElements ) {
			// hide new items while they are loading
			var $newElems = $( newElements ).css({ opacity: 0 });
			// ensure that images load before adding to masonry layout
			$newElems.imagesLoaded(function(){
			  // show elems now they are ready
			  $newElems.animate({ opacity: 1 });
			  $fluid.masonry( 'appended', $newElems, true,function(){
			  	$fluid.children('.box').fadeIn();
			        fluidHover();
			        $('.box iframe').css({ width:"100%",height:"250px"});
			        $(window).scroll(function () {
				    $('#sidebar').css('min-height',$('html').height()+'px');
			        });
			     }); 	
			    });
		      });
	         }
	 }); 		                  
    
  $('.box iframe').css({
     width:"100%",
	 height:"250"
  });
  
  //HOVER FLUID BOX
  function fluidHover(){
    $(".fluid-articles li").hover(
			function(){
				  $(this).find('p').css({
					 color:"#000"
				  });
				  $(this).css({
                     boxShadow: '0 0 5px rgba(0, 0, 0, 0.3)',
				  });
				  var imgHeight=$(this).find('img').height();
				  var imgtip=imgHeight/2-10;
				  $(this).find('.img').css('top',imgtip-25+"px");

				  $(this).find('.overlay').css("height",imgHeight+"px").stop(true,true).fadeIn(800);

			},function(){
				  $(this).find('p').css({
					 color:"#666"
				  });
				  $(this).css({
                     boxShadow: '0 0 5px rgba(0, 0, 0, 0.1)'
				  });
				  $(this).find('.overlay').stop(true,true).fadeOut(800);
			}
   );
   $(".fluid-articles li .overlay").click(
			function(){
				var postlink=$(this).find('a').attr('href');
				window.location.href=postlink;
			}
   );
  }fluidHover();
  
  //RESPONSIVE MENU
  $('.menu-trigger').toggle(function(){
              $('.responsive-menu ul').slideDown(100,function(){
			      $('body,html').css('overflow','hidden');
				  $('#menu-side-menu-1').css({
					   height:$(window).height()-$('.responsive-header').height()
				  });
				});
              },
              function(){$('.responsive-menu ul').hide();
			  $('body,html').css('overflow','auto');
  });
  $('#menu-side-menu-1').niceScroll({
			   cursorcolor:"#666",
			   scrollspeed:70,
			   horizrailenabled:false,
			   autohidemode:false
   });
  
  //TOGGLE NAVIGATION
  $('.primary-navigation>ul>li>ul').parent().addClass('dropped');
		   function menuHoverIn(){
			   $(this).children('ul').slideDown(200);
		   }
		   function menuHoverOut(){
			   $(this).children('ul').slideUp(300);
		   }
	       $('.primary-navigation > ul > li').hoverIntent({
				 sensitivity: 2,
				 interval: 200,
				 over: menuHoverIn,
				 timeout: 0,
				 out: menuHoverOut
		   });
   
   //HOVER PORTFOLIO
    $('li.portfolio-item a').hover(function(){
              $(this).children('.portfolio-item-info').stop().fadeIn(800);
              },
              function(){$(this).children('.portfolio-item-info').stop().fadeOut(300);
              });
   
   //NICE SCROLL
   $('#sidebar').niceScroll({
	   cursorcolor:"#000",
	   cursorborder:"none",
	   scrollspeed:70,
	   horizrailenabled:false,
	   autohidemode:true
   });
   
   //LOADING PORTFOLIO DETAILS
   $('#ajax-load').css({
	   height:windowHeight+'px'
   });
   $('.portfolio-item a').click(function(){
	   loadContent(this,'.single-portfolio-page');
   });
   function loadContent(obj,target){
	   var url=$(obj).attr('data-url');
	   if(url){
		   $(obj).attr('href','javascript:void(0)');
		   $('body').css('overflow','hidden');
		   $('#main-overlay-1').animate({height:'100%'},'1000','easeInCirc');
		   $('#main-overlay-2').delay(500).animate({height:'100%'},'300','easeInCirc');
		   $('#main-overlay-3').delay(700).animate({height:'100%'},'500','easeInCirc');	
		   $('html,body').animate({scrollTop:0},'fast');
		   $('#ajax-load').delay(1000).fadeIn(function(){
			  $('#ajax-close').fadeIn();
				 $('#ajax-load').niceScroll({
			      cursorcolor:"#666",
			      scrollspeed:70,
			      horizrailenabled:false,
			      autohidemode:false
				});
		   });
		   ajaxload('#ajax-content',url,target);
	   }
   }
   
   $('#ajax-close').click(function(){
	   $(this).fadeOut(function(){
		 $('#main-overlay-1').add('#main-overlay-2').css('height','0');
	     $('#ajax-load').fadeOut('300',function(){$('#ajax-content').html('');});
	     $('#main-overlay-3').delay(300).animate({height:'0'},'slow','easeInCirc',function(){$('body').css('overflow','auto');});
		
	   });
   });
   
   /*Ajax load*/
   function ajaxload(id,url,object) { 
	$(id).addClass("loading"); 
	$.ajax({ 
		type: "get", 
		url: url, 
		cache: false, 
		error: function() {$(id).html('Loading error!');}, 
		success: function(data) { 
			$(id).removeClass("loading"); 
			$(id).append($(data).find(object).html());
			/*Flex slider*/
			 $('.van_slider').flexslider({
			   slideshow:true,
			   smoothHeight: false,
			   keyboard: true,
			   multipleKeyboard: true,
			   start: function() {
					   $('.van_slider').css('background','none');
			   }
		   });
		}
	}); 
   }
   
   /*Flickr gallery*/
   $(widgetID).jflickrfeed({
	limit: number,
	qstrings: {
		id: flickrID
	},
	itemTemplate:
		'<li>' +
			'<a rel="colorbox" href="{{image}}" title="{{title}}">' +
				'<img src="{{image_s}}" alt="{{title}}" />' +
			'</a>' +
		'</li>'
	}, function() {
		$(widgetID+' a').colorbox();
	});
	
	/*Flex slider*/
   $(window).load(function(){
     $('.van_slider').flexslider({
	   slideshow:true,
	   smoothHeight: true,
	   keyboard: true,
       multipleKeyboard: true,
	   start: function() {
               $('.van_slider').css('background','none');
	          }
     });
   });
   
   /*Back to Top*/
   $(window).scroll(function(){
	  if($(document).scrollTop()===0){
		  $('#backtoTop').fadeOut();
	  }else{
	      $('#backtoTop').fadeIn();
	  } 
	});
	/*Back to Top*/
	$('#backtoTop').click(function(){
		$('html,body').animate({scrollTop:0},'slow');
		return false;
	});
   
});

// Modernizr
var Modernizr,template;
Modernizr.load({
  test: Modernizr.mq('only all'),
  yep: template+'/js/widgets.js'
});